package com.wonders.framework.util;

import java.io.File;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;

import com.wonders.framework.exception.FrameworkException;

/**
 * 文件工具
 * 
 * @author WuBin
 * 
 */
public class FileUtil {

	/**
	 * 获取文件
	 * 
	 * @param classpath
	 *            classpath
	 * @return 文件
	 */
	public static File getFileByClasspath(String classpath) {
		if (classpath == null) {
			return null;
		}
		URL url = Thread.currentThread().getContextClassLoader()
				.getResource(classpath);
		if (url == null) {
			return null;
		}
		try {
			return new File(url.toURI());
		} catch (URISyntaxException e) {
			throw new FrameworkException("文件获取失败", e);
		}
	}

	/**
	 * 获取文件流
	 * 
	 * @param classpath
	 *            classpath
	 * @return 文件
	 */
	public static InputStream getFileStreamByClasspath(String classpath) {
		if (classpath == null) {
			return null;
		}
		InputStream is = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream(classpath);
		return is;
	}

}
